from tkinter import *

def draw_polygon():
    try:
        X, Y, N = map(int, (entry_x.get(), entry_y.get(), entry_n.get()))
    except ValueError:
        error_label.config(text='Неверные значения X, Y или N')
        return

    coords = [0, 0, 10, 0, 10, 10, 20, 10, 20, 20, 10, 20, 10, 30, 0, 30, 0, 0]

    if 0 <= X <= 500 and 0 <= Y <= 500 and (N == 0 or (N > 0 and 0 * N <= X <= 500 - 40 * N and 0 * N <= Y <= 500 - 30 * N) or (N < 0 and 40 * N <= X <= 500 and 30 * N <= Y <= 500)):
        error_label.config(text='')
        canvas.delete("all")
        canvas.create_polygon(*(c * N + p for c, p in zip(coords, [X, Y] * (len(coords) // 2))), fill='blue')
    else:
        error_label.config(text='Неверные значения X, Y или N. X={}, Y={}, N={}'.format(X, Y, N))

root = Tk()
root.title("ИТ марафон")
root.geometry("500x500")

entry_x = Entry(root, width=10)
entry_y = Entry(root, width=10)
entry_n = Entry(root, width=10)

label_x = Label(root, text="X:")
label_y = Label(root, text="Y:")
label_n = Label(root, text="N:")

button_draw = Button(root, text="Нарисовать", command=draw_polygon)

label_x.grid(row=0, column=0)
label_y.grid(row=1, column=0)
label_n.grid(row=2, column=0)

entry_x.grid(row=0, column=1)
entry_y.grid(row=1, column=1)
entry_n.grid(row=2, column=1)

button_draw.grid(row=3, column=0, columnspan=2)

error_label = Label(root, text='', fg='red')
error_label.grid(row=4, column=0, columnspan=2)

canvas = Canvas(root, bg="white", width=500, height=500)
canvas.grid(row=5, column=0, columnspan=2)

root.mainloop()
